//
//  TLModalView.m
//  ModalViewDemo
//
//  Created by telen on 2016/11/22.
//  Copyright © 2016年 telen. All rights reserved.
//

#import "TLModalView.h"

@implementation TLModalView

+ (instancetype)tlmodalViewWithSuperView:(UIView *)superView
{
    TLModalView* modalView = nil;
    if (superView) {
        modalView = [[TLModalView alloc] initWithFrame:superView.bounds];
        modalView.coverColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        modalView.backgroundColor = [UIColor clearColor];
        [superView addSubview:modalView];
    }
    return modalView;
}

- (void)updateDisplay
{
    [self setNeedsDisplay];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.canTouchBack) {
        for (UIView* view in self.followViewsArr) {
            CGRect rect = [self convertRect:view.frame toView:self];
            BOOL yn = CGRectContainsPoint(rect, point);
            if (yn) {
                return NO;
            }
        }
        //
        for (NSValue* viewRect in self.followViewsRectArr) {
            CGRect rect = [self convertRect:[viewRect CGRectValue] toView:self];
            BOOL yn = CGRectContainsPoint(rect, point);
            if (yn) {
                return NO;
            }
        }
    }
    return YES;
}

- (void)drawRect:(CGRect)rect
{
    if (self.coverColor == nil) {
        return;
    }
    //// Color Declarations
    UIColor* strokeColor = [UIColor clearColor];
    UIColor* fillColor = self.coverColor;
    {
        //// 透明扣底
        UIBezierPath* bezierpath = [UIBezierPath bezierPath];
        bezierpath.miterLimit = 4;//线的衔接类型
        bezierpath.usesEvenOddFillRule = YES;
        //背景板
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(0, 0)];
        [bezierpath appendPath:path];
        [bezierpath closePath];
        //互斥区域
        for (UIView* view in self.followViewsArr) {
            CGRect rect = [self convertRect:view.frame toView:self];
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)];
            [maskPath setUsesEvenOddFillRule:YES];
            [bezierpath appendPath:maskPath];
            [bezierpath closePath];
        }
        //
        for (NSValue* viewRect in self.followViewsRectArr) {
            CGRect rect = [self convertRect:[viewRect CGRectValue] toView:self];
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)];
            [maskPath setUsesEvenOddFillRule:YES];
            [bezierpath appendPath:maskPath];
            [bezierpath closePath];
        }
        //
        [fillColor setFill];
        [bezierpath fill];
        [strokeColor setStroke];
        bezierpath.lineWidth = 0;
        [bezierpath stroke];
        
        //// 画虚线
        strokeColor = [UIColor whiteColor];
        bezierpath = [UIBezierPath bezierPath];
        bezierpath.miterLimit = 4;//线的衔接类型
        //互斥区域
        for (UIView* view in self.followViewsArr) {
            CGRect rect = [self convertRect:view.frame toView:self];
            CGFloat pod = 2;
            rect = CGRectMake(rect.origin.x-pod, rect.origin.y-pod, rect.size.width+pod*2, rect.size.height+pod*2);
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)];
            [maskPath setUsesEvenOddFillRule:YES];
            [bezierpath appendPath:maskPath];
            [bezierpath closePath];
        }
        //
        for (NSValue* viewRect in self.followViewsRectArr) {
            CGRect rect = [self convertRect:[viewRect CGRectValue] toView:self];
            CGFloat pod = 2;
            rect = CGRectMake(rect.origin.x-pod, rect.origin.y-pod, rect.size.width+pod*2, rect.size.height+pod*2);
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)];
            [maskPath setUsesEvenOddFillRule:YES];
            [bezierpath appendPath:maskPath];
            [bezierpath closePath];
        }
        //
        CGFloat dash[] = {5,2};
        [bezierpath setLineDash:dash count:2 phase:0];
        //
        [fillColor setFill];
        [strokeColor setStroke];
        bezierpath.lineWidth = 2;
        [bezierpath stroke];
        
        
    }
}

@end
